Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
Rest API return status code 201 even with a "internal error"

When a do a POST request with no body data it should return a status code 400, but it's returning a status code 201 with an empty object.

This the code:

router.post('/', (req: Request, res: Response) => {
const transaction = new Transactions({
    name: req.body?.name,
    amount: req.body?.amount,
    type: req.body?.type,
})
try{
    const newTransaction = transaction.save();
    return res.status(201).json(newTransaction);
}catch (err){
    return res.status(400).json({message: err.message});
}
})

This is the postman request and the response:

Postman request

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This looks like mongodb and mongoose. .save() returns an promise and should be resolved. You can do it with async / await here. What you return as an response is an pending promise.

router.post("/", async (req: Request, res: Response) => {
  const transaction = new Transactions({
    name: req.body?.name,
    amount: req.body?.amount,
    type: req.body?.type,
  });
  try {
    const newTransaction = await transaction.save();
    return res.status(201).json(newTransaction);
  } catch (err) {
    return res.status(400).json({ message: err.message });
  }
});
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!